package com.shaoshaohuo.app.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.HomeTabLayoutAdapter;
import com.shaoshaohuo.app.ui.fragment.CouponFragment;
import com.shaoshaohuo.app.view.TopbarView;

import java.util.ArrayList;
import java.util.List;

public class MyCouponActivity extends AppCompatActivity {
    /**
     * tobar
     */
    private ImageView order_tobar_back;
    private TextView order_tobar_enter;
    private ImageView order_tobar_right;
    private LinearLayout order_layout_topbar_left;
    private TopbarView mTopbarView;
    /**
     * tablayout
     */
    private List<String> couopn_tab_data;
    private TabLayout coupon_tabLayout;
    private ViewPager coupon_viewPager;
    private CouponFragment coupon_fragment;
    private List<Fragment> coupon_fragment_data;
    private HomeTabLayoutAdapter coupon_tab_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_coupon);
        initView();
        getTabData();
        bindAdapter();
        initListener();
    }

    private void initListener() {
        order_tobar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void bindAdapter() {
        coupon_tabLayout.setTabMode(TabLayout.MODE_FIXED);
        coupon_viewPager.setAdapter(coupon_tab_adapter);
        coupon_tabLayout.setupWithViewPager(coupon_viewPager);
    }

    private void getTabData() {
        couopn_tab_data.add("未使用");
        couopn_tab_data.add("已过期");
        Bundle bundle;
        for (int i = 0; i < couopn_tab_data.size(); i++) {
            coupon_fragment = new CouponFragment();
            coupon_fragment_data.add(coupon_fragment);
            bundle = new Bundle();
            bundle.putString("coupon", i + 1 + "");
            coupon_fragment.setArguments(bundle);
            coupon_tabLayout.addTab(coupon_tabLayout.newTab().setText(couopn_tab_data.get(i)));
        }
    }

    private void initView() {
        /**
         * tobar
         * */
        order_tobar_back = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        order_tobar_back.setVisibility(View.VISIBLE);
        order_tobar_back.setImageResource(R.mipmap.return_key);
        order_tobar_enter = (TextView) findViewById(R.id.toolbar_topname);
        order_tobar_enter.setText("优惠券");
        order_tobar_right = (ImageView) findViewById(R.id.imageview_message);
        order_tobar_right.setVisibility(View.GONE);
        order_layout_topbar_left = (LinearLayout) findViewById(R.id.layout_topbar_left);
        order_layout_topbar_left.setVisibility(View.VISIBLE);
        coupon_tabLayout = (TabLayout) findViewById(R.id.coupon_tabLayout);
        coupon_viewPager = (ViewPager) findViewById(R.id.coupon_viewPager);
        couopn_tab_data = new ArrayList<>();
        coupon_fragment_data = new ArrayList<>();
        coupon_tab_adapter = new HomeTabLayoutAdapter(getSupportFragmentManager(), this, couopn_tab_data, coupon_fragment_data);
    }

}
